<template>
  <div>
    <category :scene="scene"></category>
    <!-- 卡片区域 -->
    <el-card style="margin: 10px 0px">
      <div v-show="scene == 0">
        <el-button type="primary" icon="Plus" @click="addSpu" :disabled="categoryStore.c3Id ? false : true"> 添加SPU </el-button>
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column type="index" label="序号" width="80px" style="text-align: center"></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description"></el-table-column>
          <el-table-column label="操作">
            <!-- row:即为已有的SPU对象  @click="addSku(row)"  @click="updateSpu(row)" @click="findSku(row)" @confirm="deleteSpu(row)" :title="`你确定删除${row.spuName}?`"-->
            <template #="{ row }">
              <el-button type="primary" size="small" icon="Plus" title="添加SKU" @click="addSku(row)"></el-button>
              <el-button type="primary" size="small" icon="Edit" title="修改SPU" @click="updateSpu(row)"></el-button>
              <el-button type="primary" size="small" icon="View" title="查看SKU列表" @click="findSku(row)"></el-button>
              <el-popconfirm width="200px" :title="`你确定删除${row.spuName}?`" @confirm="deleteSpu(row)">
                <template #reference>
                  <el-button type="primary" size="small" icon="Delete" title="删除SPU"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]" :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total" @current-change="getHasSpu" @size-change="changeSize" />
      </div>
      <SpuForm ref="spu" v-show="scene == 1" @changeScene="changeScene"></SpuForm>
      <SkuForm ref="sku" v-show="scene == 2" @changeScene="changeScene"></SkuForm>
      <!-- 查看sku的对话框 -->
      <el-dialog title="查看sku" v-model="show">
        <el-table :data="skuArr" border>
          <el-table-column label="sku名字" prop="skuName"></el-table-column>
          <el-table-column label="sku价格" prop="price"></el-table-column>
          <el-table-column label="sku重量" prop="weight"></el-table-column>
          <el-table-column label="sku图片">
            <template #="{ row }">
              <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue'
import { ElMessage } from 'element-plus'
//引入分类相关的仓库
import useCategoryStore from '@/store/modules/category'
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu/index.ts'
import type { HasSpuResponseData, Records, SpuData, SkuInfoData, SkuData } from '@/api/product/spu/type'
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
let categoryStore = useCategoryStore()
let scene = ref<number>(0) //0:显示已有SPU  1:添加或者修改已有SPU 2:添加SKU的结构
//分页器默认页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(3)
//存储已有的SPU的数据
let records = ref<Records>([])
//存储已有SPU总个数
let total = ref<number>(0)
//获取子组件实例SpuForm
let spu = ref<any>()
//获取子组件实例SkuForm
let sku = ref<any>()
//存储全部的SKU数据
let skuArr = ref<SkuData[]>([])
//对话框默认不展示
let show = ref<boolean>(false)
//监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    getHasSpu()
  }
)
//此方法执行:可以获取某一个三级分类下全部的已有的SPU
let getHasSpu = async (pager = 1) => {
  //修改当前页码
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id)
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
//分页器下拉菜单发生变化的时候触发
let changeSize = () => {
  getHasSpu()
}

//添加Spu按钮的回调
let addSpu = () => {
  //切换为场景1:添加与修改已有SPU结构->SpuForm
  scene.value = 1
  spu.value.initAddSpu(categoryStore.c3Id)
}
//修改Spu按钮的回调
let updateSpu = (row: SpuData) => {
  //切换为场景1:添加与修改已有SPU结构->SpuForm
  scene.value = 1
  spu.value.initHasSpuData(row)
}
//子组件SpuForm绑定自定义事件:目前是让子组件通知父组件切换场景为0
let changeScene = (obj: any) => {
  //子组件Spuform点击取消变为场景0:展示已有的SPU
  scene.value = obj.flag
  if (obj.params == 'update') {
    getHasSpu(pageNo.value)
  } else {
    //再次获取下列表数据
    getHasSpu()
  }

  //再次获取下列表数据
  getHasSpu()
}

//添加SKU按钮的回调
let addSku = (row: SpuData) => {
  //点击添加SKU按钮切换场景为2
  scene.value = 2
  //调用子组件的方法初始化添加SKU的数据,将一级/二级id和row传过去（row包含了三级id和当前属性的id）
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
//查看已有SPU下的sku数据
let findSku = async (row: SpuData) => {
  let res: SkuInfoData = await reqSkuList(row.id as number)
  if (res.code == 200) {
    skuArr.value = res.data
    //展示对话框
    show.value = true
  }
}

//删除spu
let deleteSpu = async (row: any) => {
  let res = await reqRemoveSpu(row.id)
  if (res.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    //获取数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败'
    })
  }
}
//路由切换的时候销毁仓库分类的数据
onBeforeUnmount(() => {
  //清空仓库的数据
  categoryStore.$reset()
})
</script>

<style scoped></style>
